package com.jianzong.lingxuan.im;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @auth Leiwenlong
 * @since 2021/1/30
 *
 *  通道初始化器
 */
public class WebsocketChannelInitializer extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        //获取管道
        ChannelPipeline pipeline = socketChannel.pipeline();

        //设置http编解码器
        pipeline.addLast(new HttpServerCodec());
        //设置用于支持大数据流的支持
        pipeline.addLast(new ChunkedWriteHandler());
        //设置聚合器合器主要讲HttpMessage聚合成FullHttpRequest/Response
        pipeline.addLast(new HttpObjectAggregator(1024*64));


        //指定接手对应的路由信息
        //必须使用ws后缀结尾的url才接收处理
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));

        //设置自定义的Handler
        pipeline.addLast(new MyChatHandler());


    }

}
